/**
 * @desc: 申请提现
 * @author: binlety
 */
import React, { useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { message } from 'antd';
import CommonLoading from '@/components/CommonLoading';
import BankApply from './component/BankApply';
import { queryClassIIBindCardOp } from '@/pages/AccountModule/accountBalance/service';
import { getAccountBalanceOp, getApplyDetails, applyCashOp, applyCashAlipayOp, virtualAccountApplyCashOp, secondAccountApplyCashOp } from './service';

const ApplyCash = () => {
  const [loading, setLoading]= useState(false)
  const [obj, setObj] = useState({}); //账号详情对象
  const [resetFlag, setResetFlag]=useState(0);
  const [balanceObj, setBalance] = useState({});

  const location = useLocation();

	let accountNo;

	if(location.state?.accountNo) {
		accountNo = location.state?.accountNo;
	}
	
  useEffect(()=>{//初次进入或未绑卡再次进入
    if(location.pathname == '/account/applyCash' && !obj.openBankName){
      getApplyDetailsFunc(accountNo);
      getBalanceFunc();
    } else {
			setObj({});
		}
  },[location.pathname])

  useEffect(()=>{//提现成功
    if(resetFlag){
      getApplyDetailsFunc(accountNo)
    }
  },[resetFlag])

  const getBalanceFunc = async () => {
    const { data, success } = await getAccountBalanceOp({type: accountNo ? 2 : 1});
    if(success){
      setBalance(data || {});
    }
  }

  //获取页面展示信息
  const getApplyDetailsFunc = async (accountNo)=>{
    setLoading(true)
		const params = {};
		if(accountNo) {
			params.accountNo = accountNo;
			params.accountType = 2;
		}
    const { data = {}, success } = await getApplyDetails(params);
    if (success) {
      if(parseFloat(data.secondCashAvailBal)){
        const res = await queryClassIIBindCardOp();
        if(res.success){
          const { accountNo, bankNo, mobile, name, openBankName } = res.data || {};
          setObj({
            ...data,
            accountNo2: accountNo, 
            bankNo2: bankNo, 
            mobile2: mobile, 
            name2: name, 
            openBankName2: openBankName,
            type: data.type || 1//兼容
          });
        }
      }else{
        setObj(data);
      }
    }
    let timer = setTimeout(()=>{
      setLoading(false)
      clearInterval(timer)
      timer = null
    })
  }

  //银行卡
  const BankProps = {
    obj,
    balanceObj,
    applyCashByBank: async (params)=>{
      setLoading(true)
			if(accountNo) {
				params.accountNo = accountNo;
				params.accountType = 2;
			}
      const res = await applyCashOp(params)
      setLoading(false)
      if (res.success) {
        setResetFlag(resetFlag+1)
        message.success('提现申请已提交，请等待银行处理')
      }
    },
    applyCashAliPay: async (params)=>{
      setLoading(true)
			if(accountNo) {
				params.accountNo = accountNo;
				params.accountType = 2;
			}
      const res = await applyCashAlipayOp(params)
      setLoading(false)
      if (res.success) {
        setResetFlag(resetFlag+1)
        message.success('提现申请已提交，请等待后台审核')
      }
    },
    virtualAccountApplyCash: async (params)=>{
      setLoading(true)
			if(accountNo) {
				params.accountNo = accountNo;
			}
      const res = await virtualAccountApplyCashOp(params)
      setLoading(false)
      if (res.success) {
        setResetFlag(resetFlag+1)
        message.success('提现操作成功')
      }
    },
    secondAccountApplyCash: async (params)=>{
      setLoading(true)
			if(accountNo) {
				params.accountNo = accountNo;
				params.accountType = 2;
			}
      const res = await secondAccountApplyCashOp(params)
      setLoading(false)
      if (res.success) {
        setResetFlag(resetFlag+1)
        message.success('提现申请已提交，请等待银行处理')
      }
    },
  };

  return (
    <div className="zjf-container-nof">
      <div className='common-color3' style={{ fontSize: '18px', fontWeight:600 }}>注意事项</div>
      <p className='common-color4' style={{ fontSize:'14px', fontWeight:400, marginTop:'17px' }}>1. 请使用手机号作为安全校验码的获取方式并正确输入</p>
      <p className='common-color4' style={{ fontSize:'14px', fontWeight:400, marginTop:'12px' }}>2. 收到安全验证码后，请在30分钟内完成验证</p>
      <div className='common-color3' style={{ fontSize:'18px', fontWeight:600, margin:'26px 0 21px 0' }}>提现申请</div>
      <BankApply {...BankProps} />
      <CommonLoading loading={loading} />
    </div>
  );
};

export default React.memo(ApplyCash,()=>{
  return false
}) 
